package com.right.health.app.ui.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.utils.VerifyUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.FamilyResponse;
import com.right.health.app.common.DataCommon;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.dialog.BottomPickerDialog;
import com.right.health.app.net.FamilyApi;
import com.right.health.app.utils.EnterVerifyUtil;

import java.util.ArrayList;
import java.util.List;

/*
 * 添加家庭成员
 *
 */
public class FamilyAddActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private BottomPickerDialog<FamilyResponse.FamilyInfo> mPickerDialog;

    private TextView mChooseRelationshipTv;

    private String mChooseType;

    private EditText mNameEt, mCardIdEt, mPhoneEt;

    private FamilyApi mFamilyApi;

    private List<FamilyResponse.FamilyInfo> mDataSource = new ArrayList<>();

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.family_add);
    }

    @Override
    public void findView() {
        findViewById(R.id.tv_choose_relationship).setOnClickListener(this);
        mChooseRelationshipTv = (TextView) findViewById(R.id.tv_choose_relationship);
    }

    @Override
    public void initialize() {
        mDataSource.clear();
        mDataSource.addAll(DataCommon.getFamilyRelationship());

        mNameEt = (EditText) findViewById(R.id.et_name);
        mCardIdEt = (EditText) findViewById(R.id.et_cardId);
        mPhoneEt = (EditText) findViewById(R.id.et_phone);

        mFamilyApi = new FamilyApi();
        mFamilyApi.setListener(this);

        findViewById(R.id.tv_save).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_choose_relationship:  //选择家庭关系
                getPickerDialog().show();
                break;
            case R.id.tv_save:
                submit();
                break;
        }
    }

    private void submit() {
        if(TextUtils.isEmpty(mChooseType)) {
            ToastUtil.show(this, "请选择亲属关系");
            return;
        }
        String name = mNameEt.getText().toString().trim();
        if(TextUtils.isEmpty(name)) {
            ToastUtil.show(this, R.string.please_enter_name);
            return;
        }
        String cardId = mCardIdEt.getText().toString().trim();
        if(TextUtils.isEmpty(cardId) ) {
            ToastUtil.show(this, R.string.please_enter_id_card_number);
            return;
        } else if (cardId.length() != 18 || !EnterVerifyUtil.verifyCardid(cardId)) {
            ToastUtil.show(this, R.string.please_enter_correct_id_card_number);
            return;
        }
        String phone = mPhoneEt.getText().toString().trim();
        if(TextUtils.isEmpty(phone)) {
            ToastUtil.show(this, R.string.please_enter_phone);
            return;
        }else if (!VerifyUtil.isMobile(phone)){
            ToastUtil.show(this, R.string.please_enter_correct_phone);
        }

        mFamilyApi.addFamilyMember(mChooseType, name, cardId, phone);
    }

    private BottomPickerDialog getPickerDialog() {
        if (mPickerDialog == null) {
            mPickerDialog = new BottomPickerDialog<>(this);
            mPickerDialog.setChooseListener((tag, num) -> {
                dismissPickerDialog();
                FamilyResponse.FamilyInfo info = mDataSource.get(num);

                mChooseType = info.membership;
                mChooseRelationshipTv.setText(info.shipName);
            });
            mPickerDialog.setDataSource(mDataSource);
        }
        return mPickerDialog;
    }

    private void dismissPickerDialog() {
        if (mPickerDialog != null && mPickerDialog.isShowing()) {
            mPickerDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissPickerDialog();
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseOK()) {
            ToastUtil.show(this, "添加成功");
            sendBroadcast(BroadCastCode.REFRESH_FAMILY);
            finish();
        }
    }
}


